import {
	IAbility,
	IContext,
	IPanelButtonControllerParams,
	IParam,
} from '@/core/interface';
import { AuthUtil, deepCopy } from '@/core/utils';
import { PanelDetailController } from './panel-detail-controller';

/**
 * 面板按钮控制器
 *
 * @export
 * @class PanelButtonController
 * @extends {PanelDetailController}
 */
export class PanelButtonController extends PanelDetailController {
	/**
	 * 界面行为
	 *
	 * @type {*}
	 * @memberof PanelButtonController
	 */
	public uiAction: any;

	/**
	 * 图标方向
	 *
	 * @type {('LEFT' | 'TOP' | 'RIGHT' | 'BOTTOM')}
	 * @memberof PanelButtonController
	 */
	public iconAlign: 'LEFT' | 'TOP' | 'RIGHT' | 'BOTTOM';

	/**
	 * 是否禁用
	 *
	 * @type {boolean}
	 * @memberof PanelButtonController
	 */
	public disabled = false;

	/**
	 * @description 按钮样式
	 * @type {string}
	 * @memberof PanelButtonController
	 */
	public buttonStyle:
		| 'DEFAULT'
		| 'INVERSE'
		| 'PRIMARY'
		| 'INFO'
		| 'SUCCESS'
		| 'WARNING'
		| 'DANGER'
		| 'STYLE2'
		| 'STYLE3'
		| 'STYLE4';

	/**
	 * @description 绘制模式
	 * @type {string}
	 * @memberof PanelButtonController
	 */
	public renderMode: 'BUTTON' | 'LINK';

	/**
	 * 数据部件名称
	 *
	 * @private
	 * @type {string}
	 * @memberof PanelButtonController
	 */
	private xDataCtrlName: string;

	/**
	 * 标题值项名称
	 *
	 * @private
	 * @type {string}
	 * @memberof PanelButtonController
	 */
	public captionItemName: string;

	/**
	 * Creates an instance of PanelButtonController.
	 * @param {IPanelDetailControllerParams} opts
	 * @memberof PanelButtonController
	 */
	constructor(opts: IPanelButtonControllerParams, context: IContext, viewParams: IParam) {
		super(opts, context, viewParams);
		this.uiAction = opts.uiAction;
		this.buttonStyle = opts.buttonStyle;
		this.renderMode = opts.renderMode;
		this.iconAlign = opts.iconAlign || 'LEFT';
		this.disabled = opts.disabled;
		this.xDataCtrlName = opts.xDataCtrlName;
		this.captionItemName = opts.captionItemName;
	}

    /**
	 * 获取按钮类型
	 *
	 * @protected
	 * @return {*} 
	 * @memberof PanelButtonController
	 */
    protected getButtonType() {
        if (Object.is(this.renderMode, 'LINK')) {
            return 'link';
        } else {
            if (
                Object.is(this.buttonStyle, 'DEFAULT') ||
                Object.is(this.buttonStyle, 'STYLE2') ||
                Object.is(this.buttonStyle, 'STYLE3') ||
                Object.is(this.buttonStyle, 'STYLE4')
            ) {
                return 'default';
            } else if (Object.is(this.buttonStyle, 'DANGER')) {
                return 'error';
            } else if (Object.is(this.buttonStyle, 'INVERSE')) {
                return 'primary';
            } else {
                return this.buttonStyle.toLowerCase();
            }
        }
    }

	/**
	 * 计算行为状态权限
	 *
	 * @param {IContext} context
	 * @memberof PanelButtonController
	 */
	public async computeActionAuthState(context: IContext) {
		if (this.uiAction && this.uiAction.dataAccessAction && this.getData()) {
			const tempUIAction: any = deepCopy(this.uiAction);
			const UIService = await App.getUIService(
				tempUIAction.actionDECodeName,
				context
			);
			AuthUtil.calcActionItemAuthState(this.data, [tempUIAction], UIService, this.panel.store.viewCtx.view.getModel());
			this.visible = tempUIAction.visible;
			this.disabled = tempUIAction.disabled;
		}
	}

	/**
	 * 获取数据
	 *
	 * @private
	 * @memberof PanelButtonController
	 */
	public getData() {
		if (this.parentName) {
			let parentItem =
				this.panel.store.layoutModelDetails[`${this.parentName}_${this.index}`];
			if (!parentItem) {
				parentItem = this.panel.store.layoutModelDetails[this.parentName];
			}
			if (parentItem && parentItem.getData()) {
				this.data = parentItem.getData();
			}
		}
		return this.data;
	}

	/**
	 * 获取数据域
	 *
	 * @private
	 * @memberof PanelButtonController
	 */
	public getAbility(): IAbility {
		let xData = null;
		let curLayoutModel = null;
		Object.values(this.panel.store.layoutModelDetails).forEach(
			(layoutModel: any) => {
				if (layoutModel.name == this.name) {
					curLayoutModel = layoutModel;
				}
			}
		);
		// 获取数据容器
		if (curLayoutModel) {
			const getDataArea = (cLayoutModel: any): any => {
				let dataArea = null;
				let parentLayoutModel = null;
				Object.values(this.panel.store.layoutModelDetails).forEach(
					(pLayoutModel: any) => {
						if (pLayoutModel.name == cLayoutModel.parentName) {
							parentLayoutModel = pLayoutModel;
							if (
								parentLayoutModel.dataRegionType == 'SINGLEDATA' ||
								parentLayoutModel.dataRegionType == 'MULTIDATA'
							) {
								dataArea = parentLayoutModel.getAbility();
							}
						}
					}
				);
				if (!dataArea && parentLayoutModel) {
					dataArea = getDataArea(parentLayoutModel);
				}
				return dataArea;
			};
			xData = getDataArea(curLayoutModel);
			// 获取激活部件
			if (!xData && this.xDataCtrlName) {
				Object.values(this.panel.store.layoutModelDetails).forEach(
					(layoutModel: any) => {
						if (
							layoutModel.name.toLowerCase() == this.xDataCtrlName.toLowerCase()
						) {
							xData = layoutModel;
							return;
						}
					}
				);
			}
		}
		// 获取当前面板
		if (!xData) {
			xData = this.panel.getAbility();
		}
		return xData;
	}

	/**
	 * 加载
	 *
	 * @param {*} context
	 * @param {*} viewParams
	 * @memberof PanelButtonController
	 */
	public async load(context: IContext, viewParams: IParam) {
		await this.computeActionAuthState(context);
	}
}
